package com.jhh.pay.channel.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.jhh.pay.channel.pojo.RequestBean;
import com.jhh.pay.channel.pojo.ResponseBean;
import com.jhh.pay.channel.pojo.Structure;
import com.jhh.pay.channel.service.PayService;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@RestController
public class TradeController {
	
	@Autowired
	private PayService payService;
	
	@Value("${spring.application.version}")
	private String version;
	
	@RequestMapping("/trade/{version}/pay")
	@ResponseBody
	public ResponseBean pay(@RequestBody(required=false) RequestBean request, @PathVariable String version) {
		ResponseBean res =  payService.pay(request);
		log.info(JSON.toJSONString(res));
		if(res == null) {
			return ResponseBean.getDefault();
		}else {
			return res;
		}
		
	}
	
	@RequestMapping("/trade/{method}/state")
	@ResponseBody
	public ResponseBean state(@RequestBody(required=false) RequestBean request, @PathVariable String method) {
		request.setMethod(method);
		ResponseBean res = payService.state(request);
		if(res == null) {
			return ResponseBean.getDefault();
		}else {
			return res;
		}
	}
	
	@RequestMapping("/trade/{version}/callback_resolve")
	@ResponseBody
	public ResponseBean callbackResolve(@RequestBody(required=false) RequestBean request, @PathVariable String version) {
		return null;
	}
	
	
	@RequestMapping("/config/{version}/structure")
	@ResponseBody
	public Structure structure(@RequestBody(required=false) RequestBean request, @PathVariable String version) {
		Structure s = payService.structure(request);
		if(s == null) {
			return new Structure();
		}else {
			return s;
		}
	}
}
